//
//  BDMNaviSwitchView.m
//  welkin
//
//  Created by liyingpeng on 16/2/25.
//  Copyright © 2016年 com.baidu.cid. All rights reserved.
//

#import "BDMNaviSwitchView.h"

#define kBDMButtonUnselect SRGBCOLOR_HEX(0xfbb8b7)
#define kBDMButtonWidth (WIDTH(self) / self.titleArray.count)
#define kBDMIndicatorWidth (SCREENWIDTH / self.titleArray.count)
#define kBDMIndicatorHeight 2
#define kBDMAnimationTime 0.3

@interface BDMNaviSwitchView ()


@property(nonatomic, strong) UIView *indicatorView;
@property(nonatomic, strong) NSMutableArray *buttonArray;
@property(nonatomic, strong) NSArray *titleArray;

@property(nonatomic, strong) UIColor *titleSelectColor;
@property(nonatomic, strong) UIColor *titleNormalColor;

@property(nonatomic, copy) void (^selectBlock)(NSInteger index);

@end

@implementation BDMNaviSwitchView

- (instancetype)initWithFrame:(CGRect)frame
                       titles:(NSArray *)titleArray
                  selectBlock:(void (^)(NSInteger))selectBlock {
    return [self initWithFrame:frame titles:titleArray selectBlock:selectBlock normalColor:nil selectColor:nil];
}

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titleArray selectBlock:(void (^)(NSInteger))selectBlock normalColor:(UIColor *)normalColor selectColor:(UIColor *)selectColor {
    if (self = [super initWithFrame:frame]) {
        _titleArray = titleArray;
        self.selectBlock = selectBlock;
        self.titleSelectColor = selectColor;
        self.titleNormalColor = normalColor;
        
        _buttonArray = [[NSMutableArray alloc] initWithCapacity:titleArray.count];
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor clearColor];
    for (NSString *title in _titleArray) {
        UIButton *button = [[UIButton alloc] initWithFrame:RECT(kBDMButtonWidth * [_titleArray indexOfObject:title], 0, kBDMButtonWidth, HEIGHT(self))];
        button.exclusiveTouch = YES;
        [button.titleLabel setFont:[UIFont systemFontOfSize:kBDMBaseFontSizeSmall]];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:self.titleNormalColor ? self.titleNormalColor : kBDMButtonUnselect forState:UIControlStateNormal];
        [button setTitleColor:self.titleNormalColor ? self.titleNormalColor : kBDMButtonUnselect forState:UIControlStateHighlighted];
        [button setTitleColor:self.titleSelectColor ? self.titleSelectColor : kBDMBaseTextWhiteColor forState:UIControlStateSelected];
        [button addTarget:self action:@selector(tabButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [_buttonArray addObject:button];
    }
    
    self.currentSelectedIndex = 0;
    self.indicatorView = [[UIView alloc] initWithFrame:RECT((kBDMButtonWidth-kBDMIndicatorWidth) / 2, HEIGHT(self)-kBDMIndicatorHeight, kBDMIndicatorWidth, kBDMIndicatorHeight)];
    self.indicatorView.backgroundColor = self.titleSelectColor ? self.titleSelectColor : kBDMBaseTextWhiteColor;
    [self addSubview:self.indicatorView];
}

- (void)tabButtonAction:(UIButton *)sender {
    UIButton *tabButton = (UIButton *)sender;
    if (self.currentSelectedIndex == [self.buttonArray indexOfObject:tabButton]) {
        return;
    }
    self.currentSelectedIndex = [self.buttonArray indexOfObject:tabButton];
    if (self.selectBlock) {
        self.selectBlock(self.currentSelectedIndex);
    }
}

#pragma mark - setters & getters 

- (void)setCurrentSelectedIndex:(NSInteger)currentSelectedIndex {
    UIButton *previousButton = (UIButton *)(self.buttonArray[_currentSelectedIndex]);
    UIButton *currentButton = (UIButton *)(self.buttonArray[currentSelectedIndex]);
    previousButton.selected = NO;
    currentButton.selected = YES;
    _currentSelectedIndex = currentSelectedIndex;
    
    float indicatorX = currentSelectedIndex * kBDMButtonWidth + (kBDMButtonWidth-kBDMIndicatorWidth) / 2;
    [UIView animateWithDuration:kBDMAnimationTime animations:^{
        self.indicatorView.origin = POINT(indicatorX, HEIGHT(self)-kBDMIndicatorHeight);
    } completion:^(BOOL finish){
        
    }];
}

- (void)setCurrentSelectedIndex:(NSInteger)currentSelectedIndex animate:(BOOL)animate {
    UIButton *previousButton = (UIButton *)(self.buttonArray[_currentSelectedIndex]);
    UIButton *currentButton = (UIButton *)(self.buttonArray[currentSelectedIndex]);
    previousButton.selected = NO;
    currentButton.selected = YES;
    _currentSelectedIndex = currentSelectedIndex;
    
    float indicatorX = currentSelectedIndex * kBDMButtonWidth + (kBDMButtonWidth-kBDMIndicatorWidth) / 2;
    if (animate) {
        [UIView animateWithDuration:kBDMAnimationTime animations:^{
            self.indicatorView.origin = POINT(indicatorX, HEIGHT(self)-kBDMIndicatorHeight);
        } completion:^(BOOL finish){
            
        }];
    }
    else {
        self.indicatorView.origin = POINT(indicatorX, HEIGHT(self)-kBDMIndicatorHeight);
    }
}

@end
